package com.zx.ecode.ui.activity;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.zx.ecode.R;
import com.zx.ecode.ui.fragment.EpidemicFragment;
import com.zx.ecode.ui.fragment.IndexFragment;
import com.zx.ecode.ui.fragment.MineFragment;
import com.zzp.basepresenter.adapter.FragmentViewPagerAdapter;
import com.zzp.basepresenter.ui.activity.BaseActivity;
import com.zzp.basepresenter.ui.widgets.NoScrollViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity {
    @BindView(R.id.iv_index)
    ImageView ivIndex;
    @BindView(R.id.tv_index)
    TextView tvIndex;
    @BindView(R.id.ll_index)
    LinearLayout llIndex;
    @BindView(R.id.iv_zoology)
    ImageView ivZoology;
    @BindView(R.id.tv_zoology)
    TextView tvZoology;
    @BindView(R.id.ll_zoology)
    LinearLayout llZoology;
    @BindView(R.id.iv_mine)
    ImageView ivMine;
    @BindView(R.id.tv_mine)
    TextView tvMine;
    @BindView(R.id.ll_mine)
    LinearLayout llMine;
    @BindView(R.id.viewPager)
    NoScrollViewPager viewPager;

    private List<Fragment> fragmentList = new ArrayList<>();
    private String showFragmentTag = "indexFragment";
    private IndexFragment indexFragment;
    private EpidemicFragment epidemicFragment;
    private MineFragment mineFragment;
    public static int user = 1;

    @Override
    public int getContentView() {
        return R.layout.activity_main;
    }

    @Override
    public boolean fitWindow() {
        return false;
    }

    @Override
    public boolean softWindowAdjustPan() {
        return false;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        ButterKnife.bind(this);
        user = getIntent().getIntExtra("user", 1);
        initViewPager();
        onClick();
    }

    private void onClick() {
        llIndex.setOnClickListener(v -> {
            if (isSimpleClick()) {
                changeFragment(0, true);
            }
        });
        llZoology.setOnClickListener(v -> {
            if (isSimpleClick()) {
                changeFragment(1, true);
            }
        });
        llMine.setOnClickListener(v -> {
            if (isSimpleClick()) {
                changeFragment(2, true);
            }
        });
    }

    //初始化viewpager
    public void initViewPager() {
        if (fragmentList == null) {
            fragmentList = new ArrayList<>();
        }
        fragmentList.clear();
        indexFragment = IndexFragment.getInstance();
        epidemicFragment = EpidemicFragment.getInstance();
        mineFragment = MineFragment.getInstance();
        fragmentList.add(indexFragment);
        fragmentList.add(epidemicFragment);
        fragmentList.add(mineFragment);
        viewPager.setOffscreenPageLimit(3);
        viewPager.setAdapter(new FragmentViewPagerAdapter(getSupportFragmentManager(), viewPager, fragmentList));
    }

    //切换fragment
    private void changeFragment(int index, boolean changeFragment) {
        if (changeFragment) {
            viewPager.setCurrentItem(index, false);
        }
        retStyle();
        switch (index) {
            case 0:
                tvIndex.setTextColor(getResources().getColor(R.color.color_0B7DFE));
                ivIndex.setImageResource(R.mipmap.ic_index_press);
                break;
            case 1:
                tvZoology.setTextColor(getResources().getColor(R.color.color_0B7DFE));
                ivZoology.setImageResource(R.mipmap.ic_epidemic_press);
                break;
            case 2:
                tvMine.setTextColor(getResources().getColor(R.color.color_0B7DFE));
                ivMine.setImageResource(R.mipmap.ic_mine_press);
                break;
        }
    }

    //重置按钮颜色，图片
    public void retStyle() {
        tvIndex.setTextColor(getResources().getColor(R.color.color_999999));
        ivIndex.setImageResource(R.mipmap.ic_index);
        tvZoology.setTextColor(getResources().getColor(R.color.color_999999));
        ivZoology.setImageResource(R.mipmap.ic_epidemic);
        tvMine.setTextColor(getResources().getColor(R.color.color_999999));
        ivMine.setImageResource(R.mipmap.ic_mine);
    }
}